<template>
  <van-field class="van-signature" :label="label" :required="required" :rules="rules" :disabled="disabled">
    <template #input>
      <div class="hand-sign-box" :class="{ disabled }" :style="{ width }">
        <van-cell
          class="hand-sign"
          v-if="value == null || value === ''"
          title="添加签名"
          icon="edit"
          @click="openSignature"
        />
        <div class="sign-preview" v-else
          @touchstart.prevent="handleTouchStart"
          @touchend.prevent="handleTouchEnd"
          @mousedown.prevent="handleTouchStart"
          @mouseup.prevent="handleTouchEnd"
        >
          <div class="preview-delete-icon" @click.stop="deleteSignature">
            <van-icon name="delete-o" />
          </div>
          <img :src="value" alt="签名" :style="{ height: height }" />
        </div>
        <van-popup v-model="show" position="bottom" closeable safe-area-inset-bottom round :style="{ height: '420px' }">
          <div style="text-align: center;">
            <span style="line-height: 50px;">手写签名</span>
          </div>
          <SignaturePad height="300px" :value="value" @result="handleResult" :width="width" :autoTrim="autoTrim"/>
        </van-popup>
      </div>
    </template>
  </van-field>
</template>

<script>
import SignaturePad from './signatureDlg.vue';
import { Dialog } from 'vant';

export default {
  name: 'OnlineSignature',
  components: {
    SignaturePad
  },
  props: {
    label: {
      type: String
    },
    value: {
      type: String
    },
    required: {
      type: Boolean,
      default: false
    },
    rules: {
      type: Array
    },
    disabled: {
      type: Boolean,
      default: false
    },
    width: {
      type: String,
      default: '100%',
      required: false
    },
    height: {
      type: String,
      default: '100px',
      required: false
    },
    backgroundColor: {
      type: String,
      required: false
    },
    penColor: {
      type: String,
      default: '#000000',
      required: false
    },
    autoTrim: {
      type: Boolean,
      default: true,
      required: false
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    openSignature () {
      if (this.disabled) return;
      this.show = true;
    },
    deleteSignature () {
      Dialog.confirm({
        title: '提示',
        message: '确定要删除此签名吗？'
      })
        .then(() => {
          this.$emit('input', undefined);
          this.$emit('change', undefined);
        })
        .catch(() => {
          // on cancel
        });
    },
    handleResult (res) {
      this.$emit('input', res.data);
      this.$emit('change', res.data);
      this.show = false;
    },
    handleTouchStart (event) {
      // 模拟长按
      this.timer = setTimeout(() => {
        this.timer = null;
        this.deleteSignature();
      }, 500);
    },
    handleTouchEnd (event) {
      if (this.timer) {
        clearTimeout(this.timer);
        this.openSignature();
      }
    }
  }
};
</script>

<style scoped>
.hand-sign-box .hand-sign {
  width: 100%;
  background-color: #f7f7f7;
  text-align: left;
  -webkit-box-pack: left;
  -ms-flex-pack: left;
  justify-content: left;
  padding: 5px 10px;
}
.hand-sign-box .hand-sign i {
  margin-right: 4px;
  position: relative;
  top: 1px;
}
.hand-sign-box .hand-sign:hover {
  background-color: #efefef;
}
.hand-sign-box.disabled .hand-sign:hover {
  cursor: not-allowed;
}

.hand-sign-box .sign-preview {
  position: relative;
  min-height: 100px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  border: 1px solid #dcdfe6;
  padding: 8px 0;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.hand-sign-box .sign-preview img {
  max-width: 100%;
  cursor: pointer;
}
.hand-sign-box .sign-preview .preview-delete-icon {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 28px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  background-color: #ffffff;
  cursor: pointer;
  border-radius: 50%;
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
  display: none;
}
.hand-sign-box .sign-preview .preview-delete-icon:hover {
  -webkit-box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);
}
.hand-sign-box .sign-preview:hover .preview-delete-icon,
.hand-sign-box.is-mobile .preview-delete-icon {
  display: block;
}
.hand-sign-box.disabled .preview-delete-icon {
  display: none !important;
}
</style>
